<template>
  <div class="com_contalner">
    <el-card class="box-card" shadow="never">
      <div class="card-header">
        <div class="left">
          <el-icon><Search /> </el-icon>
          <p>筛选搜索</p>
        </div>
        <div class="rigth">
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
          <el-button type="primary" @click="onSearch"> 查询搜索 </el-button>
        </div>
      </div>
      <div>
        <el-form
          :model="form"
          ref="ruleFormRef"
          :inline="true"
          style="margin-top: 15px"
          label-width="140px"
        >
          <el-form-item label="输入搜索：" prop="id">
            <el-input
              v-model="form.id"
              placeholder="服务单号"
              style="width: 203px"
            />
          </el-form-item>
          <el-form-item label="处理状态:" prop="status">
            <el-select
              v-model="form.status"
              placeholder="全部"
              style="width: 203px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="申请时间" prop="createTime">
            <el-date-picker
              v-model="form.createTime"
              type="date"
              placeholder="请选择时间"
              style="width: 203px"
            />
          </el-form-item>

          <el-form-item label="操作人员：" prop="handleMan">
            <el-input
              v-model="form.handleMan"
              placeholder="全部"
              style="width: 203px"
            />
          </el-form-item>

          <el-form-item label="处理时间" prop="handleTime">
            <el-date-picker
              v-model="form.handleTime"
              type="date"
              placeholder="请选择时间"
              style="width: 203px"
            />
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card class="box-card" shadow="never" style="margin-top: 20px">
      <div class="left">
        <el-icon><Tickets /></el-icon>
        <p>数据列表</p>
      </div>
    </el-card>
    <div class="tab">
      <el-table
        :border="true"
        style="width: 100%"
        :model="form"
        :data="tableData"
        :cell-style="cellStyleFn"
        :header-cell-style="headerStyleFn"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="服务单号" />
        <el-table-column label="申请时间" width="180">
          <template #default="scope">
            <div>
              {{
                scope.row.createTime
                  ? dayjs(scope.row.createTime).format("YYYY-MM-DD HH:mm:ss")
                  : "/"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="memberUsername" label="用户账号" />

        <el-table-column prop="refund" label="退款金额" width="120">
          <template #default="scope">
            <div>¥{{ scope.row.productRealPrice }}</div>
          </template>
        </el-table-column>
        <el-table-column label="订申请状态" width="120">
          <template #default="scope">
            <div>
              {{
                scope.row.status == 0
                  ? "待处理"
                  : scope.row.status == 1
                  ? "退货中"
                  : scope.row.status == 2
                  ? "已完成"
                  : "已拒绝"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="处理时间" width="180">
          <template #default="scope">
            <div>
              {{
                scope.row.handleTime
                  ? dayjs(scope.row.handleTime).format("YYYY-MM-DD HH:mm:ss")
                  : ""
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="140">
          <template #default="scope">
            <el-button @click="gotoReturnApplyDetail(scope.row.id)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="bottom">
      <div class="left">
        <el-select placeholder="批量操作" style="width: 203px">
          <el-option
            v-for="item in options4"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button type="primary" style="margin-left: 10px"> 确定</el-button>
      </div>
      <div class="right">
        <!-- 分页器 -->
        <el-pagination
          v-model:currentPage="pagination.pageNum"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[1, 5, 10, 20]"
          layout="total,sizes, prev, pager, next,jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, toRefs } from "vue";
import { useRouter } from "vue-router";
import { Search, Tickets } from "@element-plus/icons-vue";
import type { FormInstance, FormRules } from "element-plus";
import useTable from "@/hooks/useTable";
import * as api from "@/request/api";
import dayjs from "dayjs"; // ES 2015
let router = useRouter();
let cellStyleFn = () => {
  return {
    textAlign: "center",
  };
};
let headerStyleFn = () => {
  return {
    textAlign: "center",
  };
};
const ruleFormRef = ref<FormInstance>();

const form = reactive({
  // 服务单号
  id: "",
  // 处理状态
  status: "",
  // 申请时间
  createTime: "",
  // 操作人员
  handleMan: "",
  // 处理时间
  handleTime: "",
});

const options = [
  {
    value: 0,
    label: "待处理",
  },
  {
    value: 1,
    label: "退货中",
  },

  {
    value: 2,
    label: "已完成",
  },
  {
    value: 3,
    label: "已拒绝",
  },
];

const options4 = [
  {
    value: "Option2",
    label: "批量操作",
  },
];

let { id, status, createTime, handleMan, handleTime } = toRefs(form);
let onSearch = () => {
  getTableData();
};

let resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  getTableData();
};

// 跳转详情页
let gotoReturnApplyDetail = (id: number) => {
  router.push({
    name: `returnApplyDetail`,
    query: {
      id: id,
    },
  });
};
let {
  tableData,
  pagination,
  handleSizeChange,
  handleCurrentChange,
  getTableData,
} = useTable(api.returnApply.getReturnApplyList, {
  id: id,
  status: status,
  createTime: createTime,
  handleMan: handleMan,
  handleTime: handleTime,
});
</script>

<style lang="scss" scoped>
.com_contalner {
  padding: 10px 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
}
.left {
  display: flex;
  align-items: center;
  p {
    margin-left: 8px;
  }
}
.tab {
  margin: 20px 0;
}
.bottom {
  display: flex;
  justify-content: space-between;
}
</style>
